package com.LeeCode;

/**
 * 完成旅途的最少时间
 */

public class Code2187 {
    public static void main(String[] args) {
        int[] time = {2};
        int totalTrips = 2;
        System.out.println(new Code2187().minimumTime(time, totalTrips));
    }

    public long minimumTime(int[] time, int totalTrips) {
        long left = 0, right = Long.MAX_VALUE;
        while (left + 1 < right) {
            long mid = left + (right - left) / 2;
            if (check(time, mid, totalTrips)) {
                right = mid;
            } else {
                left = mid;
            }
        }
        return right;
    }

    private boolean check(int[] nums, long time, int totalTrips) {
        long sum = 0;
        for (int num : nums) {
            sum += time / num;
            if (sum >= totalTrips)
                return true;
        }
        return false;
    }
}
